<?php

namespace app\controller;

use app\BaseController;
use app\model\BocMaterials;
use app\model\BocSupplier;

class Materials extends BaseController
{
    /**
     * 添加物料.
     *
     * @return \think\response\Json
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            try {
                $supplier_m = new BocSupplier();

                $order_list = $supplier_m->field('id,gys_name')->where(['gys_order' => $post['gys_order']])->find();
                if (empty($order_list['gys_name'])) {
                    throw new \Exception('供应商信息错误');
                }
                $post['gys_title'] = $order_list['gys_name'];
                $materials_m = new BocMaterials();
                $res = $materials_m->save($post);
                if (!$res) {
                    throw new \Exception($res);
                }

                return result($res);
            } catch (\Exception $e) {
                return json(['code' => 400, 'msg' => $e->getMessage()]);
            }
        }
    }

    public function list()
    {
        $page = $this->request->get('page');
        $magerials_m = new BocMaterials();
        $res = $magerials_m->paginate(5, false, [], $page);
        $data = $res->toArray();

        return json(['code' => 200, 'data' => $data]);
    }

    /*
     * 修改审核状态
     */
    public function changestate()
    {
        if ($this->request->isGet()) {
            $get = $this->request->get();
            try {
                $magerials_m = new BocMaterials();
                $state = $magerials_m->where(['id' => $get['id']])->save(['state' => $get['state']]);
                if (!$state) {
                    throw new \Exception($state);
                }

                return result($state);
            } catch (\Exception $e) {
                return json(['code' => 400, 'msg' => $e->getMessage()]);
            }
        }
    }

    public function editgetone()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $magerials_m = new BocMaterials();
            $list = $magerials_m->where(['id' => $post['id']])->find();

            return result($list);
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            try {
                $supplier_m = new BocSupplier();

                $order_list = $supplier_m->field('id,gys_name')->where(['gys_order' => $post['gys_order']])->find();
                if (empty($order_list['gys_name'])) {
                    throw new \Exception('供应商信息错误');
                }
                $post['gys_title'] = $order_list['gys_name'];
                $id = $post['id'];
                unset($post['id']);
                $magerials_m = new BocMaterials();
                $save = $magerials_m->where(['id' => $id])->save($post);
                if (!$save) {
                    throw new \Exception($save);
                }

                return result($save);
            } catch (\Exception $e) {
                return json(['code' => 400, 'msg' => $e->getMessage()]);
            }
        }
    }
}
